package com.xianzhiOA.controller;

import com.xianzhiOA.bean.XzEmp;
import com.xianzhiOA.bean.XzPlan;
import com.xianzhiOA.bean.XzProject;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzPlanService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * PlanController
 *
 * @author 甘汝雷
 * @create 2017-04-19 10:17
 **/
@Controller
@RequestMapping("/Plan")
public class XzPlanController {
    @Autowired
    private XzPlanService planService;
    @Autowired
    private XzProjectService projectService;
    @Autowired
    private XzEmpService empService;

    //查询所有项目计划
    @RequestMapping("selPlanAll.emp")
    public ModelAndView selPlanAll(XzPlan plan, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer rows,
                                   Date begina, Date beginb, String beginc, String endc) {
        ModelAndView mv = new ModelAndView("backEnd/planShow");
        PageBean<XzPlan> pageBean = planService.selAll(plan, page, rows, begina,beginb,beginc,endc);
        mv.addObject("planList", pageBean.getList());
        mv.addObject("plan", plan);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("total", pageBean.getTotal());
        mv.addObject("begina", begina);
        mv.addObject("beginb", beginb);
        mv.addObject("beginc", beginc);
        mv.addObject("endc", endc);
        return mv;
    }

    //使用ajax根据项目id和计划级别查询上级计划
    @ResponseBody
    @RequestMapping("selByPlan.emp")
    public List<XzPlan> selByPLan(Long planProject,int planRank,Long planId) {
        Map map = new HashMap();
        map.put("planProject", planProject);
        map.put("planRank", planRank - 1);
        List<XzPlan> planList = planService.selByPlan(map);
        return planList;
    }

    //使用ajax根据项目名查询项目
    @ResponseBody
    @RequestMapping("selByPName.emp")
    public List<XzProject> selByPName(String pName) {
        XzProject project = new XzProject();
        project.setProjectName(pName);
        List<XzProject> pNameList = new ArrayList<>();
        if (!pName.equals("") && pName != null) pNameList = projectService.selAll(project);
        return pNameList;
    }
    //添加项目计划
    @RequestMapping("addPlan.emp")
    public ModelAndView addPlan(XzPlan plan) {
        ModelAndView mv = errors(plan, 0);
        if (mv.getViewName().equals("redirect:selPlanAll.emp")) {
            try {
                planService.addPlan(plan);
                mv.addObject("msg", "添加成功");
            } catch (Exception e) {
                e.printStackTrace();
                mv.addObject("msg", "添加失败");
                mv.addObject("plan", plan);
                mv.setViewName("backEnd/planEdit");
            }
        }
        return mv;
    }

    //表单验证
    public ModelAndView errors(XzPlan plan,int type) {
        ModelAndView mv = new ModelAndView("redirect:selPlanAll.emp");
        Map<String, String> errors = new HashMap<>();
        if (plan.getPlanProject() == null || plan.getPlanProject() == 0) errors.put("planProject", "项目名称不能为空");
        if (plan.getPlanName() != null && plan.getPlanName() != ""){
            Map map = new HashMap();
            map.put("planName", plan.getPlanName());
            XzPlan plan1 = planService.selByName(map);
            if (plan1 != null) {
                if (type==1 && plan1.getPlanId() != plan.getPlanId()){
                    errors.put("planName", "计划名称不能重复");
                }
                if (type==0) errors.put("planName", "计划名称不能重复");
            }
        }else errors.put("planName", "计划名称不能为空");
        if (plan.getPlanStart() == null) errors.put("planStart", "开始时间不能为空");
        if (plan.getPlanEnd() == null) errors.put("planEnd", "结束时间不能为空");
        if (plan.getPlanRank() == 0) errors.put("planRank", "计划级别不能为空");
        if (errors.size() > 0) {
            mv.addObject("errors", errors);
            mv.addObject("plan", plan);
            mv.setViewName("backEnd/planEdit");
            List<XzPlan> planList = planService.selAll(new XzPlan());
            mv.addObject("planList", planList);
            return mv;
        }
        return mv;
    }

    //编辑项目计划
    @RequestMapping("planEdit.emp")
    public ModelAndView planEdit(long planId) {
        ModelAndView mv = new ModelAndView("backEnd/planEdit");
        XzPlan plan1 = new XzPlan();
        List<XzPlan> planList = planService.selAll(plan1);//返回所有计划名的集合
        if (planId != 0) {
            XzPlan plan = planService.selById(planId);
            mv.addObject("plan", plan);
        } else mv.addObject("plan", plan1);
        mv.addObject("planList", planList);
        return mv;
    }

    //修改项目计划
    @RequestMapping("updatePlan.emp")
    public ModelAndView updatePlan(XzPlan plan) {
        ModelAndView mv = errors(plan,1);
        if (mv.getViewName().equals("redirect:selPlanAll.emp")) {
            try {
                planService.update(plan);
                mv.addObject("msg", "修改成功");
            } catch (Exception e) {
                e.printStackTrace();
                mv.addObject("msg", "修改失败");
                mv.addObject("plan", plan);
                mv.setViewName("backEnd/planEdit");
            }
        }
        return mv;
    }

    //删除项目计划
    @RequestMapping("deletePlan.emp")
    public ModelAndView deletePlan(Long planId) {
        ModelAndView mv = new ModelAndView("redirect:selPlanAll.emp");
        planService.isdelete(planId);
        return mv;
    }
}
